<template>
  <div class="app-container">

    <div class="filter-container">
      <el-select v-model="noticeQuery.importance" placeholder="请选择重要等级" clearable style="width: 200px" class="filter-item" @keyup.enter.native="handleFilter">
        <el-option v-for="item in importanceOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-input v-model="noticeQuery.title" placeholder="标题" clearable style="width: 300px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker v-model="noticeQuery.begin" type="datetime" placeholder="开始时间" class="filter-item" value-format="yyyy-MM-dd HH:mm:ss" @keyup.enter.native="handleFilter" />
      <el-date-picker v-model="noticeQuery.end" type="datetime" placeholder="结束时间" class="filter-item" value-format="yyyy-MM-dd HH:mm:ss" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="区域" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.building }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="发布时间" width="300">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.publishTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="重要等级" width="300">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.importance"
            :max="3"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            :low-threshold="1"
            :high-threshold="3"
            style="display:inline-block"
          />
        </template>
      </el-table-column>
      <el-table-column label="标题" width="600">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="medium" @click="handleView(row)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="page" :limit.sync="limit" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" fullscreen>
      <div class="createPost-main-container">
        <div class="editor-content" v-html="notice.content" />
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getList, getNotice } from '@/api/notice'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      page: 1,
      limit: 10,
      noticeQuery: {
        building: '-1',
        importance: '',
        title: '',
        begin: '',
        end: ''
      },
      notice: {
        id: '',
        building: '',
        importance: '', // 重要程度
        title: '', // 文章题目
        publishTime: '', // 发布时间
        content: '' // 文章内容
      },
      dialogFormVisible: false,
      importanceOptions: [{ label: '1', key: 1 }, { label: '2', key: 2 }, { label: '3', key: 3 }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.page, this.limit, this.noticeQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.page = 1
      this.getList()
    },
    async handleView(row) {
      const res = await getNotice(row.id)
      this.notice = res.data
      console.log(this.notice)
      this.dialogFormVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-main-container {
  padding: 40px 45px 20px 50px;
  .postInfo-container {
    position: relative;
    @include clearfix;
    margin-bottom: 10px;
    .postInfo-container-item {
      float: left;
    }
  }
}
.word-counter {
  width: 40px;
  position: absolute;
  right: 10px;
  top: 0px;
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
